home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 13.3 KB | 421 lines | [TEXT/MPS ] |
- //----------------------------------------------------------------------------------------
- // UMailingApplication.cp
- // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
- //----------------------------------------------------------------------------------------
-
- #if qPowerTalk
-
- #ifndef __UMAILINGAPPLICATION__
- #include "UMailingApplication.h"
- #endif
-
- // MacApp
-
- #ifndef __UDOCUMENT__
- #include "UDocument.h"
- #endif
-
- #ifndef __UERRORMGR__
- #include "UErrorMgr.h"
- #endif
-
- #ifndef __UFILE__
- #include "UFile.h"
- #endif
-
- #ifndef __UMACAPPGLOBALS__
- #include "UMacAppGlobals.h"
- #endif
-
- #ifndef __UMACAPPUTILITIES__
- #include "UMacAppUtilities.h"
- #endif
-
- #ifndef __UMEMORY__
- #include "UMemory.h"
- #endif
-
- #ifndef __UMENUMGR__
- #include "UMenuMgr.h"
- #endif
-
- // Toolbox
-
- #ifndef __AEREGISTRY__
- #include <AERegistry.h>
- #endif
-
- // ANSI
-
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
-
- //========================================================================================
- // LOCAL variables
- //========================================================================================
-
- static AECoercionHandlerUPP pTextToDSSpecUPP;
- static AECoercionHandlerUPP pDSSpecToTextUPP;
-
- //========================================================================================
- // CLASS TMailingApplication
- //========================================================================================
- #undef Inherited
-
- #pragma segment MAOpen
- #define Inherited TApplication
-
- MA_DEFINE_CLASS_M2(TMailingApplication, Inherited, MMailing);
-
- //----------------------------------------------------------------------------------------
- // TMailingApplication constructor
- //----------------------------------------------------------------------------------------
- #pragma segment MAOpen
-
- TMailingApplication::TMailingApplication()
- {
- fMainLetterFileType = kStandardLetterFileType;
- }
-
- //----------------------------------------------------------------------------------------
- // TMailingApplication destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MAOpen
-
- TMailingApplication::~TMailingApplication()
- {
- pTextToDSSpecUPP = DisposeIfRoutineDescriptor(pTextToDSSpecUPP);
- pDSSpecToTextUPP = DisposeIfRoutineDescriptor(pDSSpecToTextUPP);
- }
-
- //----------------------------------------------------------------------------------------
- // TMailingApplication::IMailingApplication:
- //----------------------------------------------------------------------------------------
- #pragma segment MAInit
-
- void TMailingApplication::IMailingApplication(OSType itsMainFileType,
- OSType itsMailType,
- OSType itsCreator)
- {
- IApplication(itsMainFileType, itsCreator);
-
- fMainLetterFileType = itsMailType;
-
- // Install AE Coercion handlers
- AECoercionHandlerUPP handler = NULL;
- long handlerRefcon = 0;
- Boolean fromTypeIsDesc;
- Boolean isSysHandler = FALSE;
- if (AEGetCoercionHandler(typeChar, typePackedDSSpec, &handler, &handlerRefcon, &fromTypeIsDesc, isSysHandler) == errAEHandlerNotFound)
- {
- if (!pTextToDSSpecUPP)
- pTextToDSSpecUPP = (AECoercionHandlerUPP) NewAECoercePtrProc(TextToDSSpecGlue);
-
- FailOSErr(AEInstallCoercionHandler(typeChar, typePackedDSSpec, pTextToDSSpecUPP, 0, FALSE, FALSE));
- }
-
- if (AEGetCoercionHandler(typePackedDSSpec, typeChar, &handler, &handlerRefcon, &fromTypeIsDesc, isSysHandler) == errAEHandlerNotFound)
- {
- if (!pDSSpecToTextUPP)
- pDSSpecToTextUPP = (AECoercionHandlerUPP) NewAECoercePtrProc(DSSpecToTextGlue);
-
- FailOSErr(AEInstallCoercionHandler(typePackedDSSpec, typeChar, pDSSpecToTextUPP, 0, FALSE, FALSE));
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TMailingApplication::DoMenuCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment MASelCommand
-
- void TMailingApplication::DoMenuCommand(CommandNumber aCommandNumber)
- {
- if (aCommandNumber == cOpenNextLetter)
- this->DoOpenNextLetter(aCommandNumber);
- else
- Inherited::DoMenuCommand(aCommandNumber);
- }
-
- //----------------------------------------------------------------------------------------
- // TMailingApplication::DoSetupMenus:
- //----------------------------------------------------------------------------------------
- #pragma segment MAApplicationRes
-
- void TMailingApplication::DoSetupMenus()
- {
- Inherited::DoSetupMenus();
- Enable(cOpenNextLetter, HasAOCEToolBox());
-
- // We don't want the menu to say "Remove Mailer" if there aren't any documents.
- if (!fDocumentList || fDocumentList->GetSize() <= 0)
- SetIndividualCommandName(cAddMailer, kAOCEStrings, kAddMailerCmdName);
- }
-
- //----------------------------------------------------------------------------------------
- // TMailingApplication::DoToolboxEvent:
- //----------------------------------------------------------------------------------------
- #pragma segment MAApplicationRes
-
- void TMailingApplication::DoToolboxEvent(TToolboxEvent* event)
- {
- if (!this->DoMailerEvent(event))
- Inherited::DoToolboxEvent(event);
- }
-
- //----------------------------------------------------------------------------------------
- // TMailingApplication::Idle:
- //----------------------------------------------------------------------------------------
- #pragma segment MAApplicationRes
-
- void TMailingApplication::Idle(IdlePhase phase)
- {
- // Give the mail package some idle time
- DoMailerEvent(NULL);
- Inherited::Idle(phase);
- }
-
- //----------------------------------------------------------------------------------------
- // TMailingApplication::OpenOld:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOpen
-
- TDocument* TMailingApplication::OpenOld(CommandNumber itsOpenCommand, TList* aFileList)
- {
- MAVolatileInit(TDocument*, aDocument, NULL);
- MAVolatileInit(TFile*, aFile, NULL);
- MAVolatileInit(TFile*, aNewFile, NULL);
-
- MAVolatileInit(OSErr, savedError, 0);
- MAVolatileInit(long, savedMessage, 0);
-
- Size oldCodeReserve;
- Size oldMemReserve;
-
- GetReserveSize(oldCodeReserve, oldMemReserve);
-
- {
- // Put iterator in a separate block so that it will go out of scope before the end of the method.
- // We don't want the iterator's failure handler to be invoked, because its stack ptr is munged.
- // If an error occurs, we'll just save it and ReSignal it after the iterator is gone.
-
- CObjectIterator iter(aFileList);
- for (aFile = (TFile *)iter.FirstObject(); iter.More() && !savedError; aFile = (TFile *)iter.NextObject())
- {
- CommandNumber kindOfDocCmd = this->KindOfDocument(itsOpenCommand, aFile);
- // reset aDocument and aNewFile so that they no longer refer to items from the
- // last go round (important in case we fail downstream)…
- aDocument = NULL;
- aNewFile = NULL;
-
- CStr63 fileName;
- aFile->GetName(fileName); // If failure occurs the file may already be
- // freed so we will save the name just in
- // case.
-
- aFileList->Delete(aFile); // ReadStationery if successful will dispose
- // of the file for us so if failure occurs
- // this had better not be in the file list.
-
- FailInfo fi;
- Try(fi)
- {
- // Set reserve down a little to ensure that we can open existing documents
- SetReserveSize(oldCodeReserve, oldMemReserve / 2);
-
- TDocument * otherDoc = this->FindDocument(aFile);
- Boolean isStationery = aFile->IsStationery();
-
- if (otherDoc && !isStationery)
- {
- otherDoc->OpenAgain(kindOfDocCmd, aDocument);
- aFile = (TFile*)FreeIfObject(aFile);
- SetReserveSize(oldCodeReserve, oldMemReserve);
- }
- else if (this->CanOpenDocument(kindOfDocCmd, aFile))
- {
- TFile* fileOwnedByDoc = aFile;
- aFile = NULL;
- AuthIdentity localIdentity;
-
- Boolean isLetter = IsLetterFileType(fileOwnedByDoc->fFileType);
- if (!isLetter || gMailing->GetAOCEIdentity(localIdentity, TRUE) == noErr)
- {
- FailNIL(aDocument = this->DoMakeDocument(kindOfDocCmd, fileOwnedByDoc));
-
- if (isLetter)
- {
- MMailable* mailDoc = MA_DYNAMIC_CAST(MMailable, aDocument);
- if (mailDoc)
- mailDoc->ReadLetter(kForDisplay);
- }
- else
- if (!isStationery)
- aDocument->ReadDocument(kForDisplay);
- else
- {
- aNewFile = this->DoMakeFile(kindOfDocCmd);
- aDocument->ReadStationery(aNewFile);
- aFile = NULL; // This file has already been freed by ReadStationery.
- aNewFile = NULL; // Clear our reference so that if we fail
- // we don't try to free this file twice.
- }
-
- aDocument->DoMakeViews(kForDisplay);
-
- if (isLetter)
- {
- MMailable* mailDoc = MA_DYNAMIC_CAST(MMailable, aDocument);
- if (mailDoc)
- mailDoc->OpenLetter();
- }
-
- if (isStationery)
- {
- CStr255 newTitle;
- aDocument->UntitledName(newTitle);
- aDocument->SetTitle(newTitle);
- }
-
- FailSpaceIsLow(); // Fail if the document leaves us with no
- // memory
- // Set the reserve back to where it was
- SetReserveSize(oldCodeReserve, oldMemReserve);
-
- // Don't attempt to show the windows until we're sure we won't fail
- aDocument->DoPostMakeViews(kForDisplay);
- }
- }
- else
- Failure(errNotMyType, 0);
- fi.Success();
- }
- else // Recover
- {
- if (fi.message == 0)
- gErrorParm3 = fileName;
-
- // We may have ended up without a document because of a failure during document init.
- // b/c this can be the case we don't know what happened to aFile, whether it was freed
- // by the document in the failure process or orphaned. Because of this we will not free
- // aFile here.
- if (aDocument)
- aDocument = (TDocument *)(FreeIfObject(aDocument)); // The document will free the file
-
- aNewFile = (TFile*)FreeIfObject(aNewFile); // If we successfully read the
- // stationery then aNewFile will be NULL
-
- // Set the reserve back to where it was
- SetReserveSize(oldCodeReserve, oldMemReserve);
-
- // Don't want to fail here, because the CObjectIterator (with its embedded FailInfo) is
- // still in scope. Instead, save the error info until after the iterator has self-destructed.
- // FailNewMessage(fi.error, fi.message, messageOpenFailed);
- savedError = fi.error;
- savedMessage = fi.message;
- if (!savedMessage)
- savedMessage = messageOpenFailed;
- }
- }
- } // iterator will self-destruct now
-
- if (savedError != noErr) // Do we need to ReSignal a failure?
- {
- // signal the failure
- Failure(savedError, savedMessage);
- }
- return aDocument;
- }
-
- //----------------------------------------------------------------------------------------
- // TMailingApplication::GetFileTypeList:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOpen
-
- void TMailingApplication::GetFileTypeList(CommandNumber itsCommandNumber,
- TypeListHandle& typeList)
- {
- if (HasAOCEToolBox())
- {
- typeList = (TypeListHandle) NewPermHandle(8);
- (*typeList)[0] = fMainFileType;
- (*typeList)[1] = fMainLetterFileType;
- }
- else
- Inherited::GetFileTypeList(itsCommandNumber, typeList);
- }
-
- //----------------------------------------------------------------------------------------
- // TMailingApplication::DoAECreateElement:
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- void TMailingApplication::DoAECreateElement(TAppleEvent* message,
- TAppleEvent* reply)
- {
- DescType theClass = message->ReadType(keyAEObjectClass);
- if (theClass == cReplyDocument)
- {
- AuthIdentity localIdentity;
-
- if (CommandEnabled(cNew) && (gMailing->GetAOCEIdentity(localIdentity, TRUE) == noErr))
- {
- TDocument * theDoc = this->OpenNew(cNew);
- theDoc->SetPropertiesFromEvent(message); // Apply the properties if any.
-
- if (message->HasParameter(keyAEData))
- {
- if (theClass == cReplyDocument && HasAOCEToolBox())
- {
- MMailable* mailDoc = MA_DYNAMIC_CAST(MMailable, theDoc);
- if (mailDoc)
- mailDoc->DoAECreateReply(message, reply);
- }
- else
- theDoc->DoAESetData(message, reply);
- }
-
- // Return the document's object specifier
- CTempDesc theDocsSpecifier;
- theDoc->MakeObjectSpecifier(theDocsSpecifier, formName);
- if (reply->fMessage.dataHandle)
- reply->WriteParameter(keyAEResult, theDocsSpecifier);
- }
- }
- else
- Inherited::DoAECreateElement(message, reply);
- }
-
- //----------------------------------------------------------------------------------------
- // TMailingApplication::DoScriptCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- void TMailingApplication::DoScriptCommand(CommandNumber aCommandNumber,
- TAppleEvent* message,
- TAppleEvent* reply)
- {
- switch (aCommandNumber)
- {
- case cAEOpenNextLetter:
- if (HasAOCEToolBox())
- {
- TOpenNextLetterCommand* anOpenNextLetterCommand = new TOpenNextLetterCommand;
- anOpenNextLetterCommand->IOpenNextLetterCommand(aCommandNumber);
- anOpenNextLetterCommand->Process();
- }
- break;
-
- default:
- Inherited::DoScriptCommand(aCommandNumber, message, reply);
- break;
- }
- }
-
- #endif // qPowerTalk
-
- //----------------------------------------------------------------------------------------
- // End of UMailingApplication.cp
-
- #pragma segment Inline
-